package com.mellarius.activities;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;

import com.helper.BdHelper;
import com.helper.DbAdaptor;
import com.helper.FilesHelper;
import com.mellarius.R;
import com.mellarius.fragments.FragmentRepository;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class GestorActividade extends ActionBarActivity implements ActionBar.TabListener {
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
	 * derivative, which will keep every loaded fragment in memory. If this
	 * becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;
	DbAdaptor dbAdaptor;
	static boolean wifi = false;
	FilesHelper filesHelper;
	public final static String EXTRA_IP = "com.example.assignment1.IP";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.filesHelper =new FilesHelper(getBaseContext());
		dbAdaptor = new DbAdaptor(getBaseContext());
		if (BdHelper.checkDataBase(this)) {
			super.onCreate(savedInstanceState);
			setUpActionBar();
		} else {
			super.onCreate(savedInstanceState);
			File f = this.getDatabasePath("mellariusBd.db3");
			if (!f.exists()) {
				f.getParentFile().mkdir();
			}
			initializeSyncOperation();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (dbAdaptor != null) {
			dbAdaptor.getDb().close();
		}

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
		mViewPager.setCurrentItem(tab.getPosition());

	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	}

	/** instantiates the activities for apiaries */
	public void initBatchOperation(View view) {
		Intent intent = new Intent(this, BatchOperation.class);
		startActivity(intent);

	}

	public void initCalendar(View view) {
		Intent intent = new Intent(this, MyCalendarActivity.class);
		startActivity(intent);

	}

	public void initializeHiveOperation(View view) {
		Intent intent = new Intent(this, HiveOperation.class);
		startActivity(intent);

	}

	public void initIntervention(View view) {
		Intent intent = new Intent(this, Intervention.class);
		startActivity(intent);

	}

	public void initRevisions(View view) {
		Intent intent = new Intent(this, Revisions.class);
		startActivity(intent);

	}

	public void initSwarmOperation(View view) {
		Intent intent = new Intent(this, SwarmOperation.class);
		startActivity(intent);

	}

	public void initSync(View view) {
		connectWifi();
		Intent intent = new Intent(this, Synchronize.class);
		intent.putExtra(EXTRA_IP, filesHelper.getLastIp());
		startActivity(intent);
		wifi = true;

	}

	public void initSyncUsb(View view) {
		Intent intent = new Intent(this, Synchronize.class);
		startActivity(intent);
		wifi = false;

	}

	public void initializeSyncOperation() {
		connectWifi();
		Intent intent = new Intent(this, Synchronize.class);
		startActivity(intent);

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	private void setUpActionBar() {

		// Set up the action bar.
		final ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		setContentView(R.layout.ui_activity_home);

		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});
		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));

		}
	}

	private void connectWifi() {
		WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
		boolean wifiEnabled = wifiManager.isWifiEnabled();
		wifi = true;
		if (!wifiEnabled) {
			wifiManager.setWifiEnabled(true);
		}
	}

	public static boolean getWifi() {

		return wifi;
	}


	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			switch (position) {
			case 0:
				return FragmentRepository.getMainApiariesFragment();
			case 1:
				return FragmentRepository.getMainAgendaFragment();
			case 2:
				return FragmentRepository.getMainHistoricFragment();
			default:
				break;
			}
			return FragmentRepository.getMainHistoricFragment();
		}

		@Override
		public int getCount() {
			// Show 6 total pages.
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.home_menu1).toUpperCase(l);
			case 1:
				return getString(R.string.home_menu2).toUpperCase(l);
			case 2:
				return getString(R.string.home_menu3).toUpperCase(l);
			}
			return null;
		}
	}
}
	

